package com.starblink.basic.style.view.recyclerview

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

/**
 * @param horGap 横向间距
 * @param verGap 竖向间距
 */
class SimpleGapItemDecor(
    val horGap: Int = 0,
    val verGap: Int = 0,
    val withEdge: Boolean = false,
    val edgeGap: Int? = null,
) :
    RecyclerView.ItemDecoration() {

    companion object {
        private const val TAG = "SimpleGapItemDecor"
        const val VIEW_TAG_NO_EDGE = Int.MIN_VALUE
    }

    var isFirstFullSpan = false


    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        when (parent.layoutManager) {
            is GridLayoutManager -> {
                val itemCount = parent.adapter?.itemCount ?: 0
                if (itemCount == 0) return

                val layoutManager = parent.layoutManager as GridLayoutManager
                val position = parent.getChildAdapterPosition(view)
                val spanCount = layoutManager.spanCount

                val column = itemCount / spanCount + (if (itemCount % spanCount > 0) 1 else 0)
                val curColumn = position / spanCount
                val firstRow = curColumn == 0
                val lastRow = curColumn == column - 1
                val firstColumn = position % spanCount == 0
                val lastColumn = position % spanCount == spanCount - 1

                if (withEdge) {
                    outRect.left = if (firstColumn) edgeGap ?: horGap else 0
                    outRect.right = horGap
                    outRect.top = if (firstRow) edgeGap ?: verGap else 0
                    outRect.bottom = if (lastRow) edgeGap ?: verGap else verGap
                } else {
                    outRect.left = 0
                    outRect.right = if (lastColumn) 0 else horGap
                    outRect.top = 0
                    outRect.bottom = if (lastRow) 0 else verGap
                }
            }
            is LinearLayoutManager -> {
                val layoutManager = parent.layoutManager as LinearLayoutManager
                val position = parent.getChildAdapterPosition(view)
                val first = position == 0
                val last = position == ((parent.adapter?.itemCount ?: 0) - 1)
                if (layoutManager.orientation == RecyclerView.HORIZONTAL) {
                    if (withEdge) {
                        outRect.left = if (first) edgeGap ?: horGap else 0
                        outRect.right = if (last) edgeGap ?: horGap else horGap
                    } else {
                        outRect.right = if (last) 0 else horGap
                    }
                } else {
                    if (withEdge) {
                        outRect.top = if (first) edgeGap ?: verGap else 0
                        outRect.bottom = if (last) edgeGap ?: horGap else horGap
                    } else {
                        outRect.bottom = if (last) 0 else verGap
                    }
                }
            }
            is StaggeredGridLayoutManager -> {
                val itemCount = parent.adapter?.itemCount ?: 0
                if (itemCount == 0) return

                val layoutManager = parent.layoutManager as StaggeredGridLayoutManager
                val spanCount = layoutManager.spanCount

                val layoutParams: StaggeredGridLayoutManager.LayoutParams = view
                    .layoutParams as StaggeredGridLayoutManager.LayoutParams
                val spanIndex = layoutParams.spanIndex
                val position = layoutParams.bindingAdapterPosition

                val firstColumn = spanIndex == 0
                val lastColumn = spanIndex == spanCount - 1

                if (position == 0) {
                    isFirstFullSpan = layoutParams.isFullSpan
                }

                val firstRow = if (isFirstFullSpan) position == 0 else position < spanCount

                if (withEdge) {
                    if (view.getTag(VIEW_TAG_NO_EDGE) == true) {
                        outRect.left = 0
                        outRect.right = 0
                        outRect.top = 0
                    } else {
                        outRect.left = if (firstColumn || layoutParams.isFullSpan) horGap else horGap.shr(1)
                        outRect.right = if (layoutParams.isFullSpan || spanIndex == spanCount - 1) horGap else horGap.shr(1)
                        outRect.top = if (firstRow) verGap else 0
                    }
                    outRect.bottom = verGap
                } else {
                    outRect.left = if (firstColumn) horGap else horGap.shr(1)
                    outRect.right = if (lastColumn) horGap.shr(1) else horGap
                    outRect.top = if (firstRow) verGap else 0
                    outRect.bottom = verGap
                }
            }
        }

    }

}